package com.tim.proxy;

import java.lang.reflect.Method;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

public class CglibProxy implements MethodInterceptor {

	private Enhancer enhancer = new Enhancer();

	public Object getProxy(Class clz) {
		// 设置需要创建子类的类
		enhancer.setSuperclass(clz);
		enhancer.setCallback(this);
		// 返回创建的子类对象
		return enhancer.create();
	}

	public Object intercept(Object target, Method method, Object[] params,
			MethodProxy methodProxy) throws Throwable {
		PerformanceMonitoring.begin(target.getClass().getName() + "."
				+ method.getName());
		Object result = methodProxy.invokeSuper(target, params);
		PerformanceMonitoring.end();
		return result;
	}

	public static void main(String[] args) throws Exception {
		CglibProxy proxy = new CglibProxy();
		CglibReflectService service = (CglibReflectService) proxy
				.getProxy(CglibReflectService.class);
		service.analogRemoveInfo();
	}
}
